#!/bin/bash
set -e

source $(dirname $0)/version

cd $(dirname $0)/..

mkdir -p bin
if [ "$(uname)" = "Linux" ]; then
    OTHER_LINKFLAGS="-extldflags -static -s"
fi
LINKFLAGS="-X github.com/rancher/rio/pkg/version.Version=$VERSION"
LINKFLAGS="-X github.com/rancher/rio/pkg/version.GitCommit=$COMMIT $LINKFLAGS"
LINKFLAGS="-X github.com/rancher/rio/pkg/constants.ControllerImage=${REPO}/rio-controller $LINKFLAGS"
LINKFLAGS="-X github.com/rancher/rio/pkg/constants.ControllerImageTag=${TAG} $LINKFLAGS"
BUILDTAGS="static"
GOOS=linux CGO_ENABLED=0 go build -tags "$BUILDTAGS" -ldflags "$LINKFLAGS $OTHER_LINKFLAGS" -o bin/rio ./cli
if [ "$CROSS" = "true" ] && [ "$ARCH" = "amd64" ]; then
    GOOS=darwin go build -tags "$BUILDTAGS" -ldflags "$LINKFLAGS" -o bin/rio-darwin ./cli
    GOOS=windows go build -tags "$BUILDTAGS" -ldflags "$LINKFLAGS" -o bin/rio-windows ./cli
fi
GOOS=linux CGO_ENABLED=0 go build -tags "$BUILDTAGS" -ldflags "$LINKFLAGS $OTHER_LINKFLAGS" -o bin/rio-controller
